package com.wwy.consumer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.wwy.config.RabbitMqUtils;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

/**
 * @author 王伟羽
 * @date 2024/3/13 14:13
 */
@Service
public class WorkQueuesConsumerOne {
    private final static String WORK_QUEUES = "work_queues";

//    @Bean
//    public void getMessageInfoOne() {
//        Connection connection = RabbitMqUtils.getConnection();
//        if (Objects.nonNull(connection)) {
//            try {
//                Channel channel = connection.createChannel();
//                channel.queueDeclare(WORK_QUEUES, true, false, false, null);
//                //设置每次消费消息的数量
//                channel.basicQos(1);
//                DeliverCallback deliverCallback = (consumerTag, delivery) -> {
//                    String message = new String(delivery.getBody(), "UTF-8");
//                    System.out.println(" [x] 队列1消息内容 '" + message + "'");
//                    System.out.println("队列1获取到消息");
//                    //手动ACK
//                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
//                };
//                channel.basicConsume(WORK_QUEUES, false, deliverCallback, consumerTag -> {
//                    System.out.println("队列1消息消费被中断");
//                });
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//        }
//    }
//
//    @Bean
//    public void getMessageInfoTwo() {
//        Connection connection = RabbitMqUtils.getConnection();
//        if (Objects.nonNull(connection)) {
//            try {
//                Channel channel = connection.createChannel();
//                channel.queueDeclare(WORK_QUEUES, true, false, false, null);
//
//                //设置每次消费消息的数量
//                channel.basicQos(1);
//                System.out.println("队列2获取到消息");
//                DeliverCallback deliverCallback = (consumerTag, delivery) -> {
//                    String message = new String(delivery.getBody(), "UTF-8");
//                    System.out.println(" [x] 队列2消息内容 '" + message + "'");
//                    //手动ACK
//                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
//                };
//                channel.basicConsume(WORK_QUEUES, false, deliverCallback, consumerTag -> {
//                    System.out.println("队列2消息消费被中断");
//                });
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }

}
